package com.thornbird.vehiclerecycling.repository;

import com.thornbird.vehiclerecycling.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

public interface UserRepository extends JpaRepository<User, Long> {

    String CONDITION = "user_name ilike :keyword or login_name ilike :keyword or email ilike :keyword " +
            "or real_name ilike :keyword or company_name ilike :keyword";

    String FIND_SQL = "select * from users where " + CONDITION +
            " order by super_user desc, active desc, last_login_time desc";

    String COUNT_SQL = "select count(1) from users where " + CONDITION;

    @Query(nativeQuery = true, value = FIND_SQL, countQuery = COUNT_SQL)
    Page<User> findByCondition(String keyword, Pageable pageable);

    User findFirstById(Long id);

    User findFirstBySourceAndUserId(String source, String userId);

}
